package com.foreknow.service.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.dao.impl.AdminDaoImpl;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.service.GuestbookService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {
    @Override
    public List<Object> queryInfo() {
        //连接数据库
        DBManager dbManager = DBManager.getInstance();
        //调用连接数据库的方法
        Connection conn = dbManager.getConnection();
        //创建dao层的对象
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //调用AdminDaoImpl对象中的 public void setConnection(Connection connection)方法连接数据库
        guestbookDao.setConnection(conn);
        List<Object> list = null;
        try {
            list = guestbookDao.getQueryAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean addInfo(Guestbook gb) {
        //连接数据库
        DBManager dbManager = DBManager.getInstance();
        //调用连接数据库的方法
        Connection conn = dbManager.getConnection();
        //创建dao层的对象
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //调用AdminDaoImpl对象中的 public void setConnection(Connection connection)方法连接数据库
        guestbookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestbookDao.addMessage(gb);
            //处理事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public boolean deleteGuestbookById(Integer id) {
        //连接数据库
        DBManager dbManager = DBManager.getInstance();
        //调用连接数据库的方法
        Connection conn = dbManager.getConnection();
        //创建dao层的对象
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //调用AdminDaoImpl对象中的 public void setConnection(Connection connection)方法连接数据库
        guestbookDao.setConnection(conn);
        boolean isRigth = false;
        try {
            isRigth = guestbookDao.deleteById(id);
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRigth;
    }

    @Override
    public Guestbook queryGuestbookById(Integer id) {
        //连接数据库
        DBManager dbManager = DBManager.getInstance();
        //调用连接数据库的方法
        Connection conn = dbManager.getConnection();
        //创建dao层的对象
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //调用AdminDaoImpl对象中的 public void setConnection(Connection connection)方法连接数据库
        guestbookDao.setConnection(conn);
        Guestbook gb = null;
        try {
            gb = guestbookDao.queryById(id);
            conn.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return gb;
    }

    @Override
    public boolean update(Guestbook guestbook){
        //连接数据库
        DBManager dbManager = DBManager.getInstance();
        //调用连接数据库的方法
        Connection conn = dbManager.getConnection();
        //创建dao层的对象
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //调用AdminDaoImpl对象中的 public void setConnection(Connection connection)方法连接数据库
        guestbookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestbookDao.update(guestbook);
            conn.commit();
            conn.rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return isRight;
    }
}